﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Navigation;

namespace SLNavigationFrameworkEx
{
    public static partial class NavigationContextHelper
    {
        #region Public Method ParseParamAsURI

        public static Uri ParseParamAsURI(this NavigationContext nc, string queryField, string formatString)
        {
            return (nc.QueryString.ContainsKey(queryField)) ? new Uri(string.Format(formatString, nc.QueryString[queryField]), UriKind.Relative) : null;
        }

        #endregion

        #region Public Method GetParamAsString

        public static string GetParamAsString(this NavigationContext nc, string queryField)
        {
            return (nc.QueryString.ContainsKey(queryField)) ? nc.QueryString[queryField] : string.Empty;
        }

        #endregion

        #region Public Method GetParamAsInt

        public static int GetParamAsInt(this NavigationContext nc, string queryField)
        {
            return (nc.QueryString.ContainsKey(queryField)) ? System.Convert.ToInt32(nc.QueryString[queryField]) : 0;
        }

        #endregion

        #region Public Method GetParamByIdAsInt

        public static int GetParamByIdAsInt(this NavigationContext nc, int id)
        {
            return System.Convert.ToInt32(nc.GetParamById(id).Value);
        }

        #endregion

        #region Public Method IsParamKeyExists

        public static bool IsParamKeyExists(this NavigationContext nc, string name)
        {
            return nc.QueryString.Keys.Contains(name);
        }

        #endregion

        #region Public Method ParseAsUriByUriPatternWithQueryParams

        public static Uri ParseAsUriByUriPatternWithQueryParams(this NavigationContext nc, string uriPattern, List<object> args)
        {
            Uri result = null;

            if (nc == null)
            {
                return result;
            }

            List<object> valueList = new List<object>();

            foreach (var item in nc.GetQueryParamsByUriPattern(uriPattern))
            {
                valueList.Add(item.Value);
            }

            if (valueList.Count > 0)
            {
                result = UriHelper.Convert2UriByUriPattern(valueList, uriPattern, args);
            }

            return result;
        }

        #endregion

        #region Public Method GetParamById

        public static KeyValuePair<string, string> GetParamById(this NavigationContext nc, int id)
        {
            return nc.QueryString.ElementAt(id);
        }

        #endregion

        #region Public Method GetParamKeyById

        public static string GetParamKeyById(this NavigationContext nc, int id)
        {
            return nc.GetParamById(id).Key;
        }

        #endregion

        #region Public Method GetQueryParamsByUriPattern

        public static IList<KeyValuePair<string, string>> GetQueryParamsByUriPattern(this NavigationContext nc, string uriPattern)
        {
            IList<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();

            var patternParamList = UriHelper.GetParamListByUriPattern(uriPattern);

            try
            {
                if (nc != null)
                {
                    foreach (var item in nc.QueryString.Where(r => r.Key.Contains("Id")))
                    {
                        if (patternParamList.Contains("{" + item.Key + "}"))
                        {
                            result.Add(item);
                        }
                    }
                }
            }
            catch { }

            return (result);
        }

        #endregion
    }
}
